<template>
  <div class="login_container">
    <!-- 首页 -->
    <div class="sy" @click="sy">首页</div>
    <!-- 登录区 -->
    <div class="login_box">
      <!-- 头像区 -->
      <div class="right">
        <img src="../assets/img/4.png" alt="" />
      </div>
      <!-- 登录表单区 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-gerentouxiang"
          ></el-input>
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-suoding"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="danger" @click="login">登录</el-button>
          <el-button type="danger" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 文字1 -->
    <div class="wz">
      <img src="../assets/img/13.png" alt="" />
    </div>
    <div class="a">
      <img src="../assets/img/24.png" height="100%" alt="" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 这是登录表单的数据对象
      loginForm: {
        username: "",
        password: "",
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        // 验证密码是否合法
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  methods: {
    // 首页
    sy() {
      this.$router.push("/home2");
    },
    // 点击重置按钮 重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 点击登录按钮对整个表单进行校验
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // valid表示表单预验证后的结果false或true
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.status !== 200) return this.$message.error("登录失败！");
        this.$message.success("登录成功！");
        // 将登录成功后的token保存到客户端的sessionStorage中
        // 除了登录之外的其它api接口必须在登录后才能访问 token只在当前网站打开期间生效所以要保存在sessionStorage中
        window.sessionStorage.setItem("token", res.token);
        window.sessionStorage.setItem("administrator", res.message);
        // 通过编程式导航跳转到后台主页
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.sy {
  position: absolute;
  cursor: pointer;
  top: 700px;
  left: 50px;
  color: #fff;
  background-color: #b71c1c;
  width: 60px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 6px;
  border: 2px solid #b71c1c;
}
.login_container {
  user-select: none;
  position: relative;
  height: 100%;
  background-color: #fff;
  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 300px;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 10px;
    border: 2px #b71c1c solid;
    z-index: 1;
    .right {
      position: absolute;
      top: 10px;
      right: 10px;
      width: 200px;
      height: 200px;
      img {
        width: 100%;
      }
    }
    .btns {
      display: flex;
      justify-content: center;
    }
    .login_form {
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
    }
  }
  .a {
    position: absolute;
    top: 140px;
    right: 155px;
    height: 500px;
  }
  .wz {
    position: absolute;
    width: 1000px;
    top: 0px;
    left: 120px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style> 

